#!/usr/bin/python
# -*- coding: UTF-8 -*-
#@filename:bbmc_RealTime.py
#@author: wheee/RenjiaLu
#@time:2018.04.22
#@description:  实时抢座

import ConfigParser
import json
import time
import random
import os,sys
import requests
from bs4 import BeautifulSoup
from datetime import date, datetime
reload(sys) 
sys.setdefaultencoding('utf-8')


#蚌埠医学院
SCHOOLNAME = "BBMC"	

FLG_AGAIN = "AGAIN"
FLG_NEXTSEAT = "NEXTSEAT"
CONFIG_NAME = "realtime.ini"
RT_SELECTION = "bbmc_realtime"
#section标签为：[openIdConf_2018-04-08]

RUNTIME =  int(time.mktime(time.strptime(time.strftime('%Y-%m-%d',time.localtime(time.time())) \
			+" 09:08:30", "%Y-%m-%d %H:%M:%S")  ))   #"2018-03-05 11:39:19" 
READYTIME = RUNTIME - 30 #提前 0.5 分钟准备
delayTime = 10

mheaders =  {
'Host':	'wechat.v2.traceint.com',
'User-Agent':	'Mozilla/5.0 (Linux; Android 7.0; MI 5 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/043909 Mobile Safari/537.36 MicroMessenger/6.6.5.1280(0x26060536) NetType/WIFI Language/zh_CN',
'Accept':	'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,image/wxpic,image/sharpp,image/apng,*/*;q=0.8',
'Accept-Encoding':	'gzip, deflate',
'Accept-Language':	'zh-CN,en-US;q=0.8'
}
mcookies = dict(FROM_TYPE="weixin" ,wechatSESS_ID="you sessionid",
	Hm_lvt_7ecd21a13263a714793f376c18038a87="1521734275,1524209281",
	Hm_lpvt_7ecd21a13263a714793f376c18038a87=str(int(time.time())))

#=========
	
#类
class GlobalValue :

	#蚌埠医学院
	#[座位标号：坐标],不同学校的座位表不同
	R1_BMC = {
	#第一自习室座位编码 		 个
	'344':'28,43', '345':'30,41', '346':'30,42', '347':'30,43', '340':'26,42', '341':'26,43', '342':'28,41', '343':'28,42', '348':'32,41', '349':'32,42', '298':'46,33', '299':'48,30', '296':'46,31', '297':'46,32', '294':'44,33', '295':'46,30', '292':'44,31', '293':'44,32', '290':'42,33', '291':'44,30', '199':'10,34', '198':'10,33', '195':'10,30', '194':'8,34', '197':'10,32', '196':'10,31', '191':'8,31', '190':'8,30', '193':'8,33', '192':'8,32', '270':'34,35', '271':'36,30', '272':'36,31', '273':'36,32', '274':'36,33', '275':'36,34', '276':'36,35', '277':'38,30', '278':'38,31', '279':'38,32', '108':'7,21', '109':'9,19', '102':'54,12', '103':'5,19', '100':'53,12', '101':'54,10', '106':'7,19', '107':'7,20', '104':'5,20', '105':'5,21', '39':'52,6', '38':'37,8', '33':'35,6', '32':'33,8', '31':'33,6', '30':'32,8', '37':'37,6', '36':'36,8', '35':'36,6', '34':'35,8', '339':'26,41', '338':'24,43', '335':'22,43', '334':'22,42', '337':'24,42', '336':'24,41', '331':'20,42', '330':'20,41', '333':'22,41', '332':'20,43', '6':'13,8', '99':'53,10', '98':'52,12', '91':'48,11', '90':'46,13', '93':'48,13', '92':'48,12', '95':'50,12', '94':'50,11', '97':'52,10', '96':'50,13', '238':'24,33', '239':'24,34', '234':'22,35', '235':'24,30', '236':'24,31', '237':'24,32', '230':'22,31', '231':'22,32', '232':'22,33', '233':'22,34', '1':'5,7', '146':'41,20', '147':'41,21', '144':'39,21', '145':'41,19', '142':'39,19', '143':'39,20', '140':'37,20', '141':'37,21', '148':'43,19', '149':'43,20', '133':'31,19', '132':'29,20', '131':'29,19', '130':'27,19', '137':'35,19', '136':'33,20', '135':'33,19', '134':'31,20', '139':'37,19', '138':'35,20', '24':'28,8', '25':'29,6', '26':'29,8', '27':'30,6', '20':'25,8', '21':'26,6', '22':'26,8', '23':'28,6', '28':'30,8', '29':'32,6', '379':'52,42', '378':'52,41', '371':'46,43', '370':'46,42', '373':'48,42', '372':'48,41', '375':'50,41', '374':'48,43', '377':'50,43', '376':'50,42', '88':'44,13', '89':'46,12', '82':'37,13', '83':'39,12', '80':'35,13', '81':'37,12', '86':'41,13', '87':'44,12', '84':'39,13', '85':'41,12', '7':'14,6', '245':'26,34', '244':'26,33', '247':'28,30', '246':'26,35', '241':'26,30', '240':'24,35', '243':'26,32', '242':'26,31', '249':'28,32', '248':'28,31', '179':'4,30', '178':'3,32', '177':'3,30', '176':'2,32', '175':'2,30', '174':'54,16', '173':'54,14', '172':'53,16', '171':'53,14', '170':'52,16', '182':'3,36', '183':'4,34', '180':'4,32', '181':'3,34', '186':'6,31', '187':'6,32', '184':'4,36', '185':'6,30', '188':'6,33', '189':'6,34', '11':'18,6', '10':'15,8', '13':'19,6', '12':'18,8', '15':'21,6', '14':'19,8', '17':'22,6', '16':'21,8', '19':'25,6', '18':'22,8', '62':'19,12', '322':'14,42', '323':'14,43', '320':'12,43', '321':'14,41', '326':'16,43', '327':'18,41', '324':'16,41', '325':'16,42', '328':'18,42', '329':'18,43', '201':'12,30', '200':'10,35', '203':'12,32', '202':'12,31', '205':'12,34', '204':'12,33', '207':'14,30', '206':'12,35', '209':'14,32', '208':'14,31', '77':'33,12', '76':'31,13', '75':'31,12', '74':'29,13', '73':'29,12', '72':'27,13', '71':'27,12', '70':'25,13', '79':'35,12', '78':'33,13', '2':'5,8', '8':'14,8', '68':'23,13', '120':'17,20', '121':'19,19', '122':'19,20', '123':'21,19', '124':'21,20', '125':'23,19', '126':'23,20', '127':'25,19', '128':'25,20', '129':'27,20', '69':'25,12', '319':'12,42', '318':'12,41', '313':'8,42', '312':'8,41', '311':'6,43', '310':'6,42', '317':'10,43', '316':'10,42', '315':'10,41', '314':'8,43', '3':'7,7', '368':'44,43', '369':'46,41', '366':'44,41', '367':'44,42', '364':'42,42', '365':'42,43', '362':'40,43', '363':'42,41', '360':'40,41', '361':'40,42', '380':'52,43', '381':'54,41', '382':'54,42', '383':'54,43', '384':'56,41', '385':'56,42', '386':'56,43', '60':'17,13', '61':'17,14', '258':'30,35', '259':'32,30', '64':'19,14', '65':'21,12', '66':'21,13', '67':'23,12', '252':'28,35', '253':'30,30', '250':'28,33', '251':'28,34', '256':'30,33', '257':'30,34', '254':'30,31', '255':'30,32', '168':'50,21', '169':'52,14', '164':'48,20', '165':'48,21', '166':'50,19', '167':'50,20', '160':'50,15', '161':'50,16', '162':'50,17', '163':'48,19', '9':'15,6', '357':'38,41', '356':'36,43', '355':'36,42', '354':'36,41', '353':'34,43', '352':'34,42', '351':'34,41', '350':'32,43', '359':'38,43', '358':'38,42', '216':'16,33', '217':'16,34', '214':'16,31', '215':'16,32', '212':'14,35', '213':'16,30', '210':'14,33', '211':'14,34', '218':'16,35', '219':'18,30', '289':'42,32', '288':'42,31', '4':'7,8', '281':'38,34', '280':'38,33', '283':'40,31', '282':'40,30', '285':'40,33', '284':'40,32', '287':'42,30', '286':'40,34', '263':'32,34', '262':'32,33', '261':'32,32', '260':'32,31', '267':'34,32', '266':'34,31', '265':'34,30', '264':'32,35', '269':'34,34', '268':'34,33', '59':'17,12', '58':'15,14', '55':'13,14', '54':'13,13', '57':'15,13', '56':'15,12', '51':'11,13', '50':'9,14', '53':'13,12', '52':'11,14', '柱':'23,7', '63':'19,13', '115':'13,21', '114':'13,20', '117':'15,20', '116':'15,19', '111':'11,19', '110':'9,20', '113':'13,19', '112':'11,20', '119':'17,19', '118':'15,21', '308':'4,40', '309':'6,41', '300':'48,31', '301':'48,32', '302':'48,33', '303':'2,38', '304':'2,40', '305':'3,38', '306':'3,40', '307':'4,38', '229':'22,30', '228':'20,34', '227':'20,33', '226':'20,32', '225':'20,31', '224':'20,30', '223':'18,34', '222':'18,33', '221':'18,32', '220':'18,31', '151':'45,19', '150':'43,21', '153':'45,21', '152':'45,20', '155':'47,20', '154':'47,19', '157':'48,15', '156':'47,21', '159':'48,17', '158':'48,16', '48':'7,14', '49':'9,13', '46':'5,14', '47':'7,13', '44':'54,8', '45':'5,13', '42':'53,8', '43':'54,6', '40':'52,8', '41':'53,6', '5':'13,6'
	}

	R2_BMC = {
	#第二自习室座位编码		  个 
	'216':'12,33', '217':'12,34', '214':'10,40', '215':'10,41', '212':'10,38', '213':'10,39', '210':'10,36', '211':'10,37', '264':'22,36', '265':'22,37', '218':'12,35', '219':'12,36', '133':'6,21', '132':'6,20', '131':'6,19', '130':'6,18', '137':'8,19', '136':'8,18', '135':'8,17', '134':'6,22', '95':'30,4', '139':'8,21', '138':'8,20', '225':'14,33', '24':'12,6', '25':'12,7', '250':'18,40', '224':'12,41', '20':'10,11', '21':'10,12', '22':'12,4', '23':'12,5', '223':'12,40', '28':'12,10', '29':'12,11', '222':'12,39', '289':'28,34', '288':'28,33', '221':'12,38', '281':'26,35', '280':'26,34', '283':'26,37', '220':'12,37', '285':'26,39', '284':'26,38', '287':'26,41', '286':'26,40', '87':'26,10', '227':'14,35', '120':'38,7', '121':'38,8', '122':'38,9', '123':'40,4', '124':'40,5', '125':'40,6', '126':'40,7', '127':'40,8', '128':'40,9', '129':'6,17', '269':'22,41', '268':'22,40', '66':'20,12', '91':'28,7', '59':'20,5', '58':'20,4', '55':'18,10', '54':'18,9', '57':'18,12', '56':'18,11', '51':'18,6', '276':'24,39', '53':'18,8', '52':'18,7', '柱':'17,23', '63':'20,9', '298':'30,34', '299':'30,35', '296':'28,41', '297':'30,33', '294':'28,39', '295':'28,40', '292':'28,37', '293':'28,38', '290':'28,35', '291':'28,36', '85':'26,8', '201':'8,36', '319':'34,37', '318':'34,36', '199':'8,34', '198':'8,33', '200':'8,35', '195':'6,39', '194':'6,38', '197':'6,41', '196':'6,40', '191':'6,35', '190':'6,34', '193':'6,37', '192':'6,36', '115':'36,6', '114':'36,5', '117':'38,4', '116':'36,7', '111':'34,6', '110':'34,5', '113':'36,4', '112':'34,7', '278':'24,41', '205':'8,40', '313':'32,40', '5':'6,10', '119':'38,6', '118':'38,5', '84':'26,7', '204':'8,39', '251':'18,41', '207':'10,33', '256':'20,37', '206':'8,41', '226':'14,34', '257':'20,38', '27':'12,9', '3':'6,8', '254':'20,35', '7':'8,6', '312':'32,39', '92':'28,8', '255':'20,36', '308':'32,35', '309':'32,36', '300':'30,36', '301':'30,37', '302':'30,38', '303':'30,39', '304':'30,40', '26':'12,8', '306':'32,33', '305':'30,41', '245':'18,35', '244':'18,34', '108':'32,10', '109':'34,4', '241':'16,40', '240':'16,39', '243':'18,33', '242':'16,41', '102':'32,4', '103':'32,5', '100':'30,9', '101':'30,10', '106':'32,8', '107':'32,9', '104':'32,6', '105':'32,7', '39':'14,12', '38':'14,11', '33':'14,6', '32':'14,5', '270':'24,33', '30':'12,12', '37':'14,10', '247':'18,37', '35':'14,8', '34':'14,7', '246':'18,36', '88':'28,4', '窗':'40,50', '8':'8,7', '282':'26,36', '252':'20,33', '72':'22,9', '271':'24,34', '86':'26,9', '70':'22,7', '330':'40,41', '60':'20,6', '61':'20,7', '258':'20,39', '259':'20,40', '64':'20,10', '65':'20,11', '179':'34,21', '178':'34,20', '177':'34,19', '176':'32,22', '175':'32,21', '174':'32,20', '173':'30,22', '172':'30,21', '171':'30,20', '170':'28,22', '203':'8,38', '96':'30,5', '69':'22,6', '249':'18,39', '253':'20,34', '248':'18,38', '182':'36,20', '183':'36,21', '180':'34,22', '181':'36,19', '186':'38,20', '187':'40,19', '184':'36,22', '185':'38,19', '188':'40,20', '189':'6,33', '311':'32,38', '202':'8,37', '4':'6,9', '310':'32,37', '97':'30,6', '83':'26,6', '317':'34,35', '6':'6,11', '315':'34,33', '316':'34,34', '99':'30,8', '98':'30,7', '168':'28,20', '169':'28,21', '229':'14,37', '228':'14,36', '164':'24,22', '165':'26,20', '166':'26,21', '167':'26,22', '160':'22,21', '161':'22,22', '162':'24,20', '163':'24,21', '11':'8,10', '10':'8,9', '13':'10,4', '12':'8,11', '15':'10,6', '14':'10,5', '17':'10,8', '16':'10,7', '19':'10,10', '18':'10,9', '314':'32,41', '272':'24,35', '93':'28,9', '273':'24,36', '274':'24,37', '31':'14,4', '275':'24,38', '151':'16,21', '150':'16,20', '153':'18,20', '152':'16,22', '155':'18,22', '154':'18,21', '157':'20,21', '156':'20,20', '159':'22,20', '158':'20,22', '62':'20,8', '277':'24,40', '36':'14,9', '82':'26,5', '322':'36,34', '90':'28,6', '238':'16,37', '239':'16,38', '279':'26,33', '234':'16,33', '235':'16,34', '236':'16,35', '237':'16,36', '230':'14,38', '231':'14,39', '232':'14,40', '233':'14,41', '81':'26,4', '48':'16,12', '49':'18,4', '46':'16,10', '47':'16,11', '44':'16,8', '45':'16,9', '42':'16,6', '43':'16,7', '40':'16,4', '41':'16,5', '1':'6,6', '323':'36,35', '320':'34,38', '321':'36,33', '326':'36,38', '327':'38,40', '324':'36,36', '325':'36,37', '9':'8,8', '328':'38,41', '329':'40,40', '146':'12,22', '147':'14,20', '144':'12,20', '145':'12,21', '142':'10,21', '143':'10,22', '140':'8,22', '141':'10,20', '209':'10,35', '208':'10,34', '307':'32,34', '148':'14,21', '149':'14,22', '77':'24,7', '76':'24,6', '75':'24,5', '74':'24,4', '73':'22,10', '50':'18,5', '71':'22,8', '68':'22,5', '94':'28,10', '79':'24,9', '78':'24,8', '2':'6,7', '263':'22,35', '80':'24,10', '262':'22,34', '261':'22,33', '89':'28,5', '260':'20,41', '267':'22,39', '67':'22,4', '266':'22,38' 
	}
	#[自习室编号:服务器代号]
	DICT_ROOMKV={"1":"176","2":"175"}

	DICT_SCHOOL={
					"1":{"ROOMVALUE":"176","SEATVALUE":R1_BMC},
					"2":{"ROOMVALUE":"175","SEATVALUE":R2_BMC},
					"RUNTIME":"06:30:00"
					}
			

	def __init__(self):
		pass


#订座函数 #ERROR  #FAILED 都要重试
def reserveSeatFunc(mheaders,mcookies,roomValue,seatValue,roomId,seatId):

	roomId = roomValue
	seatId = seatValue

	url_zuowei ="http://wechat.v2.traceint.com/index.php/reserve/index.html?f=wechat"
	url_dixzixishi = "http://wechat.v2.traceint.com/index.php/reserve/layout/libid=%s.html&1524209079"%(roomValue)
	url_querenxuanzuo = "http://wechat.v2.traceint.com/index.php/reserve/get/libid=%s&key=%s&yzm="%(roomValue,seatValue)

	requests.adapters.DEFAULT_RETRIES = 5
	rs = requests.Session()
	rs.keep_alive = False

	#点击【确认选座】 抢座位！！！
	try:
		respone=rs.get(url_querenxuanzuo,timeout=5,headers=mheaders,cookies=mcookies)
	except Exception as e:
		print(u"[E]: 确认选座出错 %s"%repr(e))
		return "#ERROR"
	else:
		#是预定成功 而不是 预订成功
		if (respone.status_code == 200) and ((respone.text.find("预定成功") >= 0) or (respone.text.find("预定座位成功") >= 0)) :
			print(u"[I]: %s 的第 %s 自习室 %s 号位置抢座成功!-状态码：%5d"% (SCHOOLNAME,roomId,seatId,respone.status_code)+"\n")
			return "#SUCCESS"
		else:
			if respone.text.find(u"已经预定") >= 0:
				print(u"[I]: %s 的第 %s 自习室 %s 号位置抢座失败!你已经预定了其他座位!-状态码：%5d"% (SCHOOLNAME,roomId,seatId,respone.status_code)+"\n")
			elif respone.text.find("被人预定") >= 0:
				print(u"[I]: %s 的第 %s 自习室 %s 号位置抢座失败!该座位已经被人预定了!-状态码：%5d"% (SCHOOLNAME,roomId,seatId,respone.status_code)+"\n")
			else:
				print(u"[I]: %s 的第 %s 自习室 %s 号位置抢座失败!未知原因-状态码：%5d"% (SCHOOLNAME,roomId,seatId,respone.status_code)+"\n")
				print(respone.text)

			return "#FAILED" #抢座失败


#判断有没有空位
def getValidSeat(mheaders,mcookies):

	url_zuowei ="http://wechat.v2.traceint.com/index.php/reserve/index.html?f=wechat"
	url_di1zixishi = "http://wechat.v2.traceint.com/index.php/reserve/layout/libid=176.html&1524369642"
	url_di2zixishi = "http://wechat.v2.traceint.com/index.php/reserve/layout/libid=175.html&1524369642"
	listRoomValue = ["176","175"]
	#listRoomValue = ["323","324"]

	requests.adapters.DEFAULT_RETRIES = 5
	rs = requests.Session()
	rs.keep_alive = False

	for urlx in listRoomValue:
		url_dixzixishi = "http://wechat.v2.traceint.com/index.php/reserve/layout/libid="+urlx+".html&1524369642"
		#点击【确认选座】 抢座位！！！
		try: 
			respone=rs.get(url_dixzixishi,timeout=5,headers=mheaders,cookies=mcookies)
		except Exception as e:
			print(u"[E]: 获取座位状况表出错 %s"%repr(e))
			time.sleep(2)
		else:
			#html_doc = open("./centosFiles/bengbuyixueyuan_2.html",'r')
			html_doc = respone.text.replace("\n"," ")
			soup = BeautifulSoup(html_doc, 'lxml')
			listInfo4 = soup.select("div[class='layout_grid']",limit = 1) 

			divinfo  = BeautifulSoup(str(listInfo4[0]), 'lxml').select("div[class='grid_cell grid_1']") 
			#print(len(divinfo))

			for ainfo in divinfo:
				if str(ainfo).find("em") >= 0 and ainfo.get_text().strip() != "" :
					value = ainfo.get("data-key")	
					print(u"[I]: 有空余位置!")
					return urlx,value #"178,90"


	#print(u"[I]: 自习室遍历完毕，没有发现空位或者获取座位状况表出错.")
	return "#NOTFOUND","#NOTFOUND" #两个参数

#验证sessionid是否失效 0-失效 1-有效
def isValid(mheaders,mcookies):

	url_zuowei ="http://wechat.v2.traceint.com/index.php/reserve/index.html?f=wechat"

	requests.adapters.DEFAULT_RETRIES = 5
	rs = requests.Session()
	rs.keep_alive = False

	#点击【座位】进入首页
	try:
		respone=rs.get(url_zuowei,timeout=5,headers=mheaders,cookies=mcookies)
	except Exception as e:
		print(u"[E]: sessionid验证出错 %s"%repr(e))
		return 1 #sessionid有效，重试
	else:
		if (respone.status_code == 200 ) and ((respone.text.find("出入口") >= 0 or (respone.text.find("自习室")))):
			print(u"[I]: sessionid验证成功-状态码：%5d"% respone.status_code)
			return 1
		else:
			print(u"[I]: sessionid验证失败-状态码：%5d"% respone.status_code)
			return 0

#开始
if __name__ == '__main__':
	
	print(u"\n[I]: %s 实时抢座监听中...\n"%SCHOOLNAME)
	todaySection = "openIdConf_%s"%time.strftime('%Y-%m-%d',time.localtime(time.time()))
	RT_SELECTION = todaySection
	#mpath = "" #windows 中的相对路径
	mpath = "/root/test_wx/" #linux 中的绝对路径
	if not(os.path.isfile(mpath+CONFIG_NAME)):
		#文件不存在
		print(u"[E]: 配置文件不存在!")
		sys.exit(0)	

	cnt_i = 0
	cnt_error = 0
	cnt_tasksuccess = 0
	cnt_taskfailed = 0 # 执行失败且被删除的任务，failed + success = 总任务数量

	while True :
		cnt_i = cnt_i + 1
		print(u"\n[I]: 当前时间 %s ,i= %d ,error= %d ,tasksuccess= %d ,taskfailed= %d ;准备执行任务."%\
			((time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))),cnt_i,cnt_error,cnt_tasksuccess,cnt_taskfailed))
		
		#定时关闭程序
		closetime =  int(time.mktime(time.strptime(time.strftime('%Y-%m-%d',time.localtime(time.time())) \
			+" 22:00:01", "%Y-%m-%d %H:%M:%S")  ))   #"2018-03-05 11:39:19" 
		if int(time.time()) - closetime > 0 :
			print(u"[E]: 到达关闭程序的时间，程序关闭")
			sys.exit(0)


		#任务准备阶段
		try:
			#获取 实时抢座任务列表
			conf = ConfigParser.ConfigParser()
			conf.read(mpath+CONFIG_NAME)       # 文件路径
			list_tmp = conf.items(RT_SELECTION)# 返回openId_wechatSESS_ID标签项下面的键值对 list

			if len(list_tmp) < 1 :
				print(u"[I]: 当前任务列表为空,程序进入休眠 20s后继续...")
				time.sleep(20)
				continue

			sessionId = list_tmp[0][1].split(",")[0].strip()
			openId = list_tmp[0][0].strip()
			mcookies["wechatSESS_ID"]  = sessionId
			#print(type(list_tmp),list_tmp[0][1]) #list_tmp[0][0] ,list_tmp[0][1] ==> k,v 格式已处理验证过，必为正确格式
			
		except Exception as e:
			cnt_error = cnt_error + 1
			print(u"[E]: 任务准备阶段出错，几秒后重试 %s"%repr(e))
			time.sleep(10)
			continue

		#任务执行阶段
		try:

			#一个用户的sessionid使用30分钟，中途若抢座成功 或 sessionid失效则 或 超过30 分钟则删除此任务，继续循环
			worktime = 1800 #秒
			t_kaishi = int(time.time())
			t_xianzai= int(time.time())
			nextJudgeTime = t_xianzai 
			cnt_NOTFOUND = 0
			while t_xianzai - t_kaishi < worktime :

				#每五分钟判断 sessionid是否失效
				if t_xianzai >= nextJudgeTime :
					nextJudgeTime = t_xianzai + 300 #5分钟后再验证
					if isValid(mheaders,mcookies) == 0 :
						#失效 删除此任务并退出此循环
						cnt_taskfailed = cnt_taskfailed + 1
						conf.remove_option(RT_SELECTION,openId)
						conf.write(open(CONFIG_NAME, "w"))
						print(u"[I]: sessionid已经失效，此任务已删除，切换下一个任务.")
						break
				
				#循环判断有没有空位
				roomValue,seatValue = getValidSeat(mheaders,mcookies) # 返回 "175","180,90" 或 "#NOTFOUND","#NOTFOUND"
				if seatValue == "#NOTFOUND" :
					#没有空位，更新现在的时间，进入下一次循环继续监听
					cnt_NOTFOUND = cnt_NOTFOUND + 1
					if cnt_NOTFOUND % 10 == 0:

						time.sleep(5)
					else:
						time.sleep(0.5)

					t_xianzai = int(time.time()) 
					print(u"[I]: cnt_NOTFOUND= %6d ,自习室遍历完毕，没有发现空位或者获取座位状况表出错."%cnt_NOTFOUND)
					continue
				else:
					#有空位，抢座！！！
					resultflg = reserveSeatFunc(mheaders,mcookies,roomValue,seatValue,"x","x")
					if resultflg == "#SUCCESS" :
						#抢座成功，删除此任务，退出当前任务的循环
						conf.remove_option(RT_SELECTION,openId)
						conf.write(open(CONFIG_NAME, "w"))
						print(u"[I]: 抢座成功！此任务已删除，切换下一个任务.")
						cnt_tasksuccess = cnt_tasksuccess + 1
						break
					else:
						# #ERROR 或者 #FAILED ,更新现在的时间，进入下一次循环继续监听
						t_xianzai = int(time.time()) 
						continue

			else:
				#单个任务超时，若后续还有任务则删除此任务，执行下一个任务
				if (len(list_tmp) > 1 ):
					conf.remove_option(RT_SELECTION,openId)
					cnt_taskfailed = cnt_taskfailed + 1
					conf.write(open(CONFIG_NAME, "w"))
					print(u"[I]: 任务超时，此任务已删除，切换下一个任务.")
				else:
					#没有后续任务，休息几秒后继续执行
					print(u"[I]: 任务超时，没有后续任务，休息几秒后继续执行.")
					time.sleep(10)


		except Exception as e:
			cnt_error = cnt_error + 1
			print(u"[E]: 任务执行阶段出错，休眠几秒后继续 %s"%repr(e))
			time.sleep(10)


			 
	print(u"\n[I]: 程序结束!\n")